package com.ziyun.pop.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.common.e.DeleteEnum;
import com.ziyun.pop.common.e.order.OrderInfoStatusEnum;
import com.ziyun.pop.common.e.order.PayTypeEnum;
import com.ziyun.pop.common.e.order.ThirdMerchantTypeEnum;
import com.ziyun.pop.common.utils.ErpOrderUtils;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.modules.order.dao.ErpOrderInfoDao;
import com.ziyun.pop.modules.order.entity.*;
import com.ziyun.pop.modules.order.service.ErpOrderAllotRelevanceService;
import com.ziyun.pop.modules.order.service.ErpOrderService;
import com.ziyun.pop.modules.order.service.OrderInfoService;
import com.ziyun.pop.modules.order.service.OrderItemService;
import com.ziyun.pop.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.pop.modules.production.service.ErpOrderItemService;
import com.ziyun.pop.modules.supp.vo.SellerCustomerApiVo;
import com.ziyun.pop.modules.supp.vo.SellerOrderApiVo;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("orderInfoService")
public class OrderInfoServiceImpl extends ServiceImpl<ErpOrderInfoDao, OrderInfoEntity> implements OrderInfoService {
    /**
     * 日志
     */
    private  static Logger logger = LoggerFactory.getLogger(OrderInfoServiceImpl.class);

    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private ErpOrderItemService erpOrderItemService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ErpOrderAllotRelevanceService erpOrderAllotRelevanceService;

    /**
     * 根据会员ID查询订单
     * @param memberId
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer memberId) {
        Page<OrderInfoEntity> page= (Page<OrderInfoEntity>) this.page(
                new Query<OrderInfoEntity>(params).getPage(),
                new QueryWrapper<OrderInfoEntity>().eq("member_id",memberId)
        );

        return new PageUtils(page);
    }

    @Override
    public R queryPageMallOrderList(Map<String, Object> params) {
        // 默认查询活动订单
        int totalCount = this.baseMapper.countMallOrder(params);

        int current = Integer.valueOf((String)params.get("page"));
        int size = Integer.valueOf((String)params.get("limit"));
        params.put("offset", (current - 1) * size);
        params.put("limit", size);
        List<OrderInfoVo> list = this.baseMapper.queryPageMallOrderList(params);

        // 存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("payTypeEnum", JsonUtils.enumToList(PayTypeEnum.class));
        mapEnum.put("orderInfoStatusEnum", JsonUtils.enumToList(OrderInfoStatusEnum.class));
        mapEnum.put("thirdMerchantTypeEnum", JsonUtils.enumToList(ThirdMerchantTypeEnum.class));

        R r = R.ok();
        r.put("page", new PageUtils(list, totalCount, size, current));
        r.put("userData", mapEnum);
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R produce(String orderId) {
        ErpOrderEntity erpOrder = erpOrderService.getById(orderId);
        if (erpOrder != null){
            return R.error("门店生产单已存在");
        }

        OrderInfoEntity orderInfo = this.getById(orderId);
        ErpOrderEntity erpOrderEntity = ErpOrderUtils.ziyunOrderToErp(orderInfo);
        erpOrderService.save(erpOrderEntity);
        List<OrderItemEntity> orderItemEntities = orderItemService.selectByOrderId(Long.valueOf(orderId));
        List<ErpOrderItemEntity> erpOrderItemEntities = ErpOrderUtils.ziyunOrderItemToErp(orderItemEntities, orderId);
        erpOrderItemService.saveBatch(erpOrderItemEntities);
        return R.ok();
    }

    @Override
    public boolean updateByMap(Map<String, Object> map, String orderId) {
        int update = this.baseMapper.updateByMap(orderId, map);
        if (update > 0){
            return true;
        }
        return false;
    }

    @Override
    public void saveAllotMerchantOrder(OrderInfoEntity orderInfo, List<OrderItemEntity> orderItemList) {
        this.save(orderInfo);
        orderItemService.saveBatch(orderItemList);
    }

    @Override
    public R printGoods(String orderNo) {
        //根据分配的单号查询原订单号
        ErpOrderAllotRelevanceEntity erpOrderAllotRelevanceEntity = erpOrderAllotRelevanceService.getOne(new QueryWrapper<ErpOrderAllotRelevanceEntity>().eq("order_allot_no", orderNo)
                .eq("del", DeleteEnum.NOT_DELETE.toInt()));
        if(erpOrderAllotRelevanceEntity==null){
            return R.error("查找原始订单信息错误,请联系管理员!");
        }
        String orderId=erpOrderAllotRelevanceEntity.getOriginalOrderNo();
        // 订单信息
        OrderInfoVo orderInfo = this.baseMapper.selectOrderInfoVoById(Long.valueOf(orderId));
        // 商品列表
        List<OrderItemEntity> orderItemList = orderItemService.selectByOrderId(Long.valueOf(orderId));
        return R.ok().put("orderInfo", orderInfo)
                .put("orderItemList", orderItemList);
    }

    @Override
    public SellerOrderApiVo getSellerOrder(Long sellerId) {
        SellerOrderApiVo sellerOrder = baseMapper.getSellerOrder(sellerId);
        return sellerOrder;
    }

    @Override
    public SellerCustomerApiVo getSellerCustomer(Long sellerId) {
        SellerCustomerApiVo sellerCustomerApiVo = baseMapper.getSellerCustomer(sellerId);
        return sellerCustomerApiVo;
    }

}
